package com.cnc.servlet;

import com.cnc.entity.Admin;
import com.cnc.service.Impl.UserServiceImpl;
import com.cnc.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/UserServlet")
public class UserServlet extends BaseServlet {
    UserService service = new UserServiceImpl();

    protected void login(HttpServletRequest req,HttpServletResponse resp)throws ServletException, IOException{
        String user = req.getParameter("username");
        String pwd = req.getParameter("password");
        String rem = req.getParameter("rem");
        if (service.login(user, pwd)) {
            if (rem != null) {
                Cookie cookie1 = new Cookie("username", user);
                Cookie cookie2 = new Cookie("password", pwd);
                Cookie cookie3 = new Cookie("rem", rem);
                cookie1.setMaxAge(60*60*24*30);
                cookie2.setMaxAge(60*60*24*30);
                cookie3.setMaxAge(60*60*24*30);
                resp.addCookie(cookie1);
                resp.addCookie(cookie2);
                resp.addCookie(cookie3);
            } else {
                Cookie cookie1 = new Cookie("username", user);
                Cookie cookie2 = new Cookie("password", pwd);
                Cookie cookie3 = new Cookie("rem", rem);
                cookie1.setMaxAge(0);
                cookie2.setMaxAge(0);
                cookie3.setMaxAge(0);
                resp.addCookie(cookie1);
                resp.addCookie(cookie2);
                resp.addCookie(cookie3);
            }
            HttpSession session = req.getSession();
            session.setAttribute("login",user);
            resp.sendRedirect("./StudentServlet?flag=show");
        } else {
            req.setAttribute("msg", "用户名或密码错误！");
            req.getRequestDispatcher("index.jsp").forward(req, resp);
        }
    }
    protected void register (HttpServletRequest req,HttpServletResponse resp)throws ServletException, IOException{
        String user = req.getParameter("username");
        String pwd1 = req.getParameter("pwd1");
        String pwd2 = req.getParameter("pwd2");
        if (user == "" || pwd1 == "" || pwd2 == "") {
            req.setAttribute("msg", "用户名，密码，确认密码不能为空！");
            req.getRequestDispatcher("register.jsp").forward(req, resp);
        } else {
            if (service.isHaveAdmin(user)) {
                req.setAttribute("msg", "该用户已存在！");
                req.getRequestDispatcher("register.jsp").forward(req, resp);
            } else {
                if (pwd1.equals(pwd2)) {
                    if (service.insertAdmin(new Admin(user, pwd1))) {
                        req.setAttribute("msg", "注册成功，请登录！");
                        req.getRequestDispatcher("index.jsp").forward(req, resp);
                    } else {
                        req.setAttribute("msg", "注册失败，请重试！");
                        req.getRequestDispatcher("register.jsp").forward(req, resp);
                    }
                } else {
                    req.setAttribute("msg", "密码和确认密码不一致！");
                    req.getRequestDispatcher("register.jsp").forward(req, resp);
                }
            }
        }
    }
}
